﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace BAL
{
    public partial class AdjustPosition : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();

       private int i = 1;
       private string CWPID = "";
       private string JPID ="";
       private string ID;
       private int Floor= 1;
       private string  PkgsBarCode = "";
       private string PkgsBarCodes = "";
       private string Sarte;
       DataTable dt = new DataTable();
        public AdjustPosition()
        {
            InitializeComponent();
        }

        public AdjustPosition(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
           
            this.tb_PLACECODE.Focus();

        }

       private void SetValue()
       {       
            tb_JPLACECODE.Text ="";
            tb_PkgsBarCode.Text ="";            
            tb_PkgsBarCode.Focus();          
       }

        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "AdjustPosition";

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "序号"; 
            cl5.MappingName = "row";
            cl5.Width = 25;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "包装码";
            cl1.MappingName = "PkgsBarCode";
            cl1.Width = 85;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "仓位名";
            cl3.MappingName = "PlaceName";
            cl3.Width = 40;
            dts.GridColumnStyles.Add(cl3);
            //DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            //cl2.HeaderText = "仓位码";
            //cl2.MappingName = "PlaceBarCode";
            //cl2.Width = 50;
            //dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "层";
            cl4.MappingName = "Floor";
            cl4.Width = 25;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "助记码";
            cl2.MappingName = "HelperCode";
            cl2.Width = 50;
            dts.GridColumnStyles.Add(cl2);
            

            dataGrid1.TableStyles.Add(dts);
        }

        public void BindData()
        {
            DataSet ds = bjs.GetAdjustPosition(CWPID);
            
            dt = ds.Tables[0];
            if (dt.Rows.Count == 0)
            {
                button4.Visible = true;
            }
            else
            {
                button4.Visible = false;
            }

            dataGrid1.DataSource = dt;
        }


        private void tb_CWPID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //仓位码是否是数字
                Regex regex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
                Match resultD = regex.Match(Convert.ToString(tb_PLACECODE.Text.Trim()));
                if (!resultD.Success)
                {
                    MessageBox.Show("仓位码只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    tb_PLACECODE.Focus();
                    return;
                }
                string PlaceCode = tb_PLACECODE.Text.ToString().Trim();
                bool result = false;
                string PlaceName ="";

                if (PlaceCode != null && PlaceCode != "")
                {
                    result = bjs.GetPIDbyPlaceCode(PlaceCode, out CWPID, out PlaceName);
                    if (result == true)
                    {
                        this.tb_PkgsBarCode.Focus();
                        this.tb_Floor.Text = "";
                        tb_PLACECODE.Text = PlaceName;
                        txtwkid.Text = PlaceCode;
                        DataGridStyle();
                        BindData();
                    }
                    else
                    {
                        MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }    
                }
                else
                {
                    MessageBox.Show("请扫描仓位码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }            
                
            }
        }

       
          
        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            { 
                bool result = false;
                string JPlaceCode =  "";
                int t_Floor =1;
                int dex = 0;
                
                PkgsBarCode = tb_PkgsBarCode.Text.Trim();
                if (PkgsBarCode == null || PkgsBarCode == "")
                {
                    MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                  }

                dex = PkgsBarCodes.IndexOf(PkgsBarCode);//如果条码已经扫描了，则不可以扫描
                 if (dex == -1)
                 {
                     PkgsBarCodes =PkgsBarCodes+ PkgsBarCode + ',';

                     result = bjs.GetJPIDbyPkgsBarCode(PkgsBarCode, out JPlaceCode, out JPID, out t_Floor);
                     if (result == true)
                     {
                         tb_JPLACECODE.Text = JPlaceCode;

                         if (i == 1)//第一次扫码
                         {
                             //判断当库位没有信息时默认从1层开始
                             if (dt.Rows.Count == 0)
                             {
                                 tb_Floor.Text = "1";
                             }
                             else
                             {
                                 tb_Floor.Text = t_Floor.ToString();
                             }
                         }
                         else
                         {
                             if (tb_Floor.Text != "")
                             {
                                 Floor = int.Parse(tb_Floor.Text);
                                 tb_Floor.Text = (Floor + 1).ToString();
                             }
                             else
                             {
                                 Floor = t_Floor;
                                 tb_Floor.Text = t_Floor.ToString();
                             }
                             
                         }
                         i = i + 1;
                         //判断当库位没有信息时执行此操作
                         if (dt.Rows.Count == 0)
                         {
                             button4.Focus();

                         }
                         else
                         {
                             button1.Focus();
                         }
                     }
                     else
                     {
                         MessageBox.Show("包装码不符合层调整,请查看包装码在库存中的状态！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                         tb_PkgsBarCode.Text = "";
                         PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                         return;
                     }
                 }
                 else
                 {
                     MessageBox.Show("该包装码已经扫描过！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                     tb_PkgsBarCode.Text = "";
                     return;
                 }               
            }  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Main main = new Main(this.ID);
            main.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string state = "";
            string Floor1 = "";
            PkgsBarCode = tb_PkgsBarCode.Text.Trim();
            if (PkgsBarCode == null || PkgsBarCode == "")
            {
                MessageBox.Show("包装码不能为空,请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
          
            string JPLACECODE = tb_JPLACECODE.Text.Trim();
            if (JPLACECODE == null || JPLACECODE == "")
            {
                MessageBox.Show("件仓位码不能为空,请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            //仓位码是否是数字
            Regex regex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
            //Match resultD = regex.Match(Convert.ToString(tb_PLACECODE.Text.Trim()));
            //if (!resultD.Success)
            //{
            //    MessageBox.Show("仓位码只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            //    tb_PLACECODE.Focus();
            //    return;
            //}
            //层是否是数字
            Match resultc = regex.Match(Convert.ToString(tb_Floor.Text.Trim()));
            if (!resultc.Success)
            {
                MessageBox.Show("层只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_Floor.Focus();
                return;
            }
            if(CWPID ==JPID)//扫描库位与件条码库位一致
            {
                Floor = int.Parse(tb_Floor.Text);
                state = bjs.UPdateSTOCKFloor(PkgsBarCode, CWPID, Floor);
                if(state=="1")
                {                
                   DataGridStyle();
                   BindData();
                   SetValue();
                  
                }
                else
                {
                    MessageBox.Show("更新库位失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    Floor = int.Parse(tb_Floor.Text);
                    tb_Floor.Text = (Floor - 1).ToString();
                    SetValue();
                    return;
                }

            }
            else
            {
                 if(i==2)
                 {
                     MessageBox.Show("首次扫描时,必须扫描本库位的包装码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                     i = 1;
                     PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                     SetValue();
                     this.tb_Floor.Text = "";
                     return;
                 }
                else
                 {
                     DialogResult dr = MessageBox.Show("该包装码不属于该库位，点击‘是’插入本库位，点击‘否’放弃操作！", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    if(dr==DialogResult.Yes)
                    {
                        bool statefalg = bjs.GetStkis(PkgsBarCode, out Sarte);
                        if (statefalg)
                        {
                            if (Sarte == "88")
                            {
                                MessageBox.Show("此包装信息是锁库未出库状态只能调整层，不能进行库位调整！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                                PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                                tb_Floor.Text = "";
                                this.tb_PkgsBarCode.Focus();
                                SetValue();
                                return;
                            }
                            if (Sarte == "1")
                            {
                                MessageBox.Show("此包装信息是锁库状态只能调整层，不能进行库位调整！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                                PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                                tb_Floor.Text = "";
                                this.tb_PkgsBarCode.Focus();
                                SetValue();
                                return;
                            }
                        }
                         Floor = int.Parse(tb_Floor.Text);
                         state = bjs.UPdateSTOCKFloor(PkgsBarCode,CWPID,Floor);
                         if (state == "1")
                         {
                             DataGridStyle();
                             BindData();
                             SetValue();
                             
                         }
                         else
                         {
                             MessageBox.Show("更新库位失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                             SetValue();
                             Floor = int.Parse(tb_Floor.Text);
                             tb_Floor.Text = (Floor - 1).ToString();
                             return;
                         }
                    }
                    else 
                    {
                        PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                        SetValue();
                        Floor = int.Parse(tb_Floor.Text);
                        tb_Floor.Text = (Floor - 1).ToString();
                        return;
                    }
                 }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
                      
            DataTable dt = new DataTable();           
            dt = null;
            dataGrid1.DataSource = dt;
          
            tb_JPLACECODE.Text = "";
            tb_PkgsBarCode.Text = "";
            tb_Floor.Text = "";
            tb_PLACECODE.Text = "";
            tb_PLACECODE.Focus();
        }
        /// <summary>
        /// 调整当当前库位没有物件的时候从一层开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string state = "";
            PkgsBarCode = tb_PkgsBarCode.Text.Trim();
            if (PkgsBarCode == null || PkgsBarCode == "")
            {
                MessageBox.Show("包装码不能为空,请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }

            string JPLACECODE = tb_JPLACECODE.Text.Trim();
            if (JPLACECODE == null || JPLACECODE == "")
            {
                MessageBox.Show("件仓位码不能为空,请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            string PLACECODE = tb_PLACECODE.Text.Trim();
            if (PLACECODE == null || PLACECODE == "")
            {
                MessageBox.Show("仓位码不能为空,请扫描仓位码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            //仓位码是否是数字
            Regex regex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
            //Match resultD = regex.Match(Convert.ToString(tb_PLACECODE.Text.Trim()));
            //if (!resultD.Success)
            //{
            //    MessageBox.Show("仓位码只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            //    tb_PLACECODE.Focus();
            //    return;
            //}
            //层是否是数字
            Match resultc = regex.Match(Convert.ToString(tb_Floor.Text.Trim()));
            if (!resultc.Success)
            {
                MessageBox.Show("层只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_Floor.Focus();
                return;
            }
             DialogResult dr = MessageBox.Show("该包装码不属于该库位，点击‘是’插入本库位，点击‘否’放弃操作！", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
             if (dr == DialogResult.Yes)
             {
                 bool statefalg = bjs.GetStkis(PkgsBarCode, out Sarte);
                 if (statefalg)
                 {
                     if (Sarte == "88")
                     {
                         MessageBox.Show("此包装信息是锁库未出库状态只能调整层，不能进行库位调整！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                         PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                         this.tb_PkgsBarCode.Focus();
                         tb_Floor.Text = "";
                         SetValue();
                         return;
                     }
                     if (Sarte == "1")
                     {
                         MessageBox.Show("此包装信息是锁库状态只能调整层，不能进行库位调整！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                         PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                         tb_Floor.Text = "";
                         this.tb_PkgsBarCode.Focus();
                         SetValue();
                         return;
                     }
                 }
                 Floor = int.Parse(tb_Floor.Text);
                 state = bjs.UPdateSTOCKFloor(PkgsBarCode, CWPID, Floor);
                 if (state == "1")
                 {
                     DataGridStyle();
                     BindData();
                     SetValue();

                 }
                 else
                 {
                     MessageBox.Show("更新库位失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                     Floor = int.Parse(tb_Floor.Text);
                     SetValue();
                     return;
                 }
             }
             else
             {
                 PkgsBarCodes = PkgsBarCodes.Replace(PkgsBarCode, "");
                 SetValue();
             }

        }
        //层跳转按钮
        private void tb_Floor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //判断当库位没有信息时执行此操作
                if (dt.Rows.Count == 0)
                {
                    button4.Focus();

                }
                else
                {
                    button1.Focus();
                }
            }
        }
       
    }
}